
<?php
include("db/connect.php");
$sumPlus=0;
$sumMinus=0;
$sumVat=0;
$sumAll=0;
$year=date("Y");
$thisyear=date("Y");
if(!empty($_GET["report_Year"])){
	$year=$_GET["report_Year"];
	}

$arrCate=array('มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน','กรกฏาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม');
$strCate="";
	for($i=0;$i<12;$i++){
	$strCate.="'".$arrCate[$i]."',";
	}

$sqlPlus="select SUM(qty*pro_price) AS sumMonth,MONTH(orders_date) As atMonth from orders,cart,product where orders.orders_id=cart.orderid and cart.pro_id=product.pro_id and YEAR(orders_date)='".$year."'  group by MONTH(orders_date)";
$queryPlus=mysql_query($sqlPlus);
$arrPlus=array(0,0,0,0,0,0,0,0,0,0,0,0);
$strPlus="";
while($resultPlus=mysql_fetch_array($queryPlus)){
	$arrPlus[$resultPlus["atMonth"]-1]=$resultPlus["sumMonth"];
	}
for($i=0;$i<12;$i++){
	$strPlus.=$arrPlus[$i].",";
	$sumPlus+=$arrPlus[$i];
	}
	
$sqlMinus="select SUM(mat_qty*mat_price) AS sumMonth,MONTH(ordersmat_date) As atMonth from ordersmaterial,cartmaterial,material where ordersmaterial.ordermat_id=cartmaterial.ordermat_id and cartmaterial.mat_id=material.mat_id and YEAR(ordersmat_date)='".$year."'  group by MONTH(ordersmat_date)";
$queryMinus=mysql_query($sqlMinus);

$arrMinus=array(0,0,0,0,0,0,0,0,0,0,0,0);
$strMinus="";
while($resultMinus=mysql_fetch_array($queryMinus)){
	$arrMinus[$resultMinus["atMonth"]-1]=$resultMinus["sumMonth"];
	}	
	for($i=0;$i<12;$i++){
	$strMinus.=$arrMinus[$i].",";
	$sumMinus+=$arrMinus[$i];
	}
	
$arrVat=array(0,0,0,0,0,0,0,0,0,0,0,0);
$strVat="";	
	for($i=0;$i<12;$i++){
	$strVat.=(($arrPlus[$i]*7)/100).",";
	$sumVat+=(($arrPlus[$i]*7)/100);
	}
	
	
$arrAvr=array(0,0,0,0,0,0,0,0,0,0,0,0);
$strAvr="";	
	for($i=0;$i<12;$i++){
	$strAvr.=(($arrPlus[$i]+$arrMinus[$i])/2).",";
	}
	
$sumAll=$sumPlus+$sumMinus+$sumVat;


?>
		
		<style type="text/css">
${demo.css}
		</style>
		<script type="text/javascript">
$(function () {
	Highcharts.setOptions({
    chart: {
        style: {
            fontFamily: 'Tahoma'
        }
    }
});
    newj('#container').highcharts({
		
        title: {
            text: 'รายรับ-รายจ่ายประจำปี'
        },
        xAxis: {
		
            categories: [<?php echo $strCate;?>]
        },
        labels: {
            items: [{
                html: '',
                style: {
                    left: '50px',
                    top: '18px',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
                }
            }]
        },
        series: [{
            type: 'column',
            name: 'รายรับ',
			color:'green',
            data: [<?php echo $strPlus;?>]//รายรับ
        }, {
            type: 'column',
            name: 'รายจ่าย',
			color:'blue',
            data: [<?php echo $strMinus;?>]
        }, {
            type: 'column',
            name: 'Vat',
			color:'red',
            data: [<?php echo $strVat;?>]
        }, {
            type: 'spline',
            name: 'เฉลี่ย',
			color:'yellow',
            data: [<?php echo $strAvr;?>],
            marker: {
                lineWidth: 2,
                lineColor: Highcharts.getOptions().colors[3],
                fillColor: 'black'
            }
        }, {
            type: 'pie',
            name: 'ร้อยละ',
            data: [{
                name: 'รายรับ',
                y: <?php if($sumPlus!=0)echo number_format(($sumPlus*100)/$sumAll,2,'.',''); else echo '0';?>,
               color:'green'
            }, {
                name: 'รายจ่าย',
                y: <?php if($sumMinus!=0) echo number_format(($sumMinus*100)/$sumAll,2,'.',''); else echo '0';?>,
                color:'blue'
            }, {
                name: 'Vat',
                y: <?php if($sumVat!=0)  echo number_format(($sumVat*100)/$sumAll,2,'.',''); else echo '0';?>,
                color:'red'
            }],
            center: [100, 80],
            size: 100,
            showInLegend: false,
            dataLabels: {
                enabled: true
            }
        }]
    });
});


		</script>
	
    <select class="ddl_report">
      <option>กรุณาเลือกรายงาน</option>
      <?php
   for($i=0;$i!=7;$i++){
	   ?>
	    <option value="<?php echo $thisyear-$i?>"><?php echo ($thisyear-$i+543)?></option>
    
	   <?php
	   }
	?>
     
    </select>
      <?php
    if(empty($queryPlus)){
		echo "<div> xxx</div>";
		}
	?>
<script>
$(function(){
	$(".ddl_report").change(function(){
		$("#listspan").load("graphreport.php?report_Year="+$(".ddl_report").val());
		});
	});
</script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

